package servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 *
 * servlet
 *
 * 2018/5/13-9:52
 * 2018
 * Created by wuqiang on 2018/5/13.
 */
public class ServletGetReource extends HttpServlet {
    /**
     * 使用ServletConfig对象获取资源文件的细节
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String realPath1 = getServletContext().getRealPath("/");
        //E:\MyProj\JavaWebBase\out\artifacts\JavaWebBase_war_exploded\
        String realPath2 = getServletContext().getRealPath("/WEB-INF/");
        //E:\MyProj\JavaWebBase\out\artifacts\JavaWebBase_war_exploded\WEB-INF
        resp.getOutputStream().write(realPath2.getBytes());
        //显示类加载器
        String name = getServletContext().getClassLoader().getClass().getName();
        resp.getOutputStream().write(name.getBytes());
        //使用classLoad获取资源文件，是在哪个环境获取的资源文件？
        InputStream resourceAsStream = getServletContext().getClassLoader().getResourceAsStream("db.properties");
        Properties p=new Properties();
        p.load(resourceAsStream);
        String username = p.getProperty("username");
        System.out.println(username );
    }
}